package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.exception.BusinessException;
import com.apache.ius.common.CommonUtils;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 删除（渠道商）判断或者迁移
 */
public class DelChannelPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("userId"));//渠道商ID
        Map<String, Object> result = new HashMap<String, Object>();
        String msg = "";
        if(StrUtil.isNull(userId)){
            throw new BusinessException("渠道商ID不能为空");
        }
        log.info("开始删除渠道商-->"+userId);

        //1、验证是否有余额
        Map<String,Object> wMap1 = new HashMap<>();
        wMap1.put("ssUserBalance.w_userId", userId);
        ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("obj", "s_ssUserBalance", wMap1);
        if(resultEntity1!=null && resultEntity1.getEntity()!=null){
            DataMap ubObj = (DataMap)resultEntity1.getEntity();
            double availableBalance = NumberUtils.getDouble(String.valueOf(ubObj.get("availableBalance")),0,true) ;
            double freezeBalance = NumberUtils.getDouble(String.valueOf(ubObj.get("freezeBalance")),0,true);
            log.info("可用余额[availableBalance]-->"+availableBalance);
            log.info("冻结余额[freezeBalance]-->"+availableBalance);
            if(availableBalance>0 || freezeBalance>0){
                result.put(CustomMethodPlugin.resultMark, "该账户当前还存在余额，故不能删除！");
                result.put(CustomMethodPlugin.pluginInnerReturn, "F01");
                return result;
            }
        }

        //2、验证是否存在自己会员
        Map<String,Object> wMap2 = new HashMap<>();
        wMap2.put("uctUser.w_orgId", userId);
        wMap2.put("uctUser.w_userType", "member");
        ResultEntity resultEntity2 = IusPluginUtil.getInstance().select("plateform", "count", "s_uctUser", wMap2);
        if(resultEntity2!=null && resultEntity2.getEntity()!=null){
            int memberCount = NumberUtils.getInt(String.valueOf(resultEntity2.getEntity()),0);
            log.info("下属会员个数[memberCount]-->"+memberCount);
            if(memberCount>0){
                result.put(CustomMethodPlugin.resultMark, "该账户下还有会员，故不能删除！");
                result.put(CustomMethodPlugin.pluginInnerReturn, "F02");
                return result;
            }
        }

        //3、验证是否有子级渠道商。
        Map<String,Object> wMap3 = new HashMap<>();
        wMap3.put("uctUser.w_orgId", userId);
        wMap3.put("uctUser.w_userType", "broker");
        ResultEntity resultEntity3 = IusPluginUtil.getInstance().select("plateform", "count", "s_uctUser", wMap3);
        if(resultEntity3!=null && resultEntity3.getEntity()!=null){
            int childCount = NumberUtils.getInt(String.valueOf(resultEntity3.getEntity()),0);
            log.info("子级渠道商个数[childCount]-->"+childCount);
            if(childCount>0){
                result.put(CustomMethodPlugin.resultMark, "该账户下还有子级渠道商，故不能删除！");
                result.put(CustomMethodPlugin.pluginInnerReturn, "F03");
                return result;
            }
        }

        //最后执行删除（渠道商和其对应资金对象）
        Map<String,Object> delMap = new HashMap<>();
        delMap.put("uctUser.w_userId", userId);
        ResultEntity delRes = IusPluginUtil.getInstance().saveInvoke("plateform",  "d_uctUser", delMap);
        if(delRes!=null && delRes.getEntity()!=null){
            log.info("删除渠道商完成！");
            delMap = new HashMap<>();
            delMap.put("ssUserBalance.w_userId", userId);
            delRes = IusPluginUtil.getInstance().saveInvoke(CommonUtils.getSysEname(),  "d_ssUserBalance", delMap);
            log.info("删除渠道商对应资金对象完成！");
            result.put(CustomMethodPlugin.resultMark, "渠道商删除成功！");
            result.put(CustomMethodPlugin.pluginInnerReturn, "T");
            return result;
        }else{
            result.put(CustomMethodPlugin.resultMark, "渠道商删除失败！");
            result.put(CustomMethodPlugin.pluginInnerReturn, "F00");
            return result;
        }
    }


    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
